#-*- coding: utf-8 -*-
import xmlrpclib
import pickle
from Tkinter import *

import tkSimpleDialog as tks
import helper
from orion_modele import *

class Vue(object):
    def __init__(self,parent):
        self.parent=parent
        self.root=Tk()
        self.canevas=Canvas(self.root,width=500, height=500,bg="red")
        self.canevas.bind("<Button>",self.parent.newDestination)
        self.canevas.pack()
        self.cadreEnvoie=Frame(self.root)
        self.id=Entry(self.cadreEnvoie)
        self.id.pack(side=LEFT)
        b=Button(self.cadreEnvoie,text="Demarre Partie",command=self.parent.demarre)
        b.pack() 
        self.cadreEnvoie.pack()
        
    def drawMap(self,civs):
        self.canevas.delete(ALL)
        for i in civs:
            for j in civs[i].vaisseaux:
                self.canevas.create_oval(j.x,j.y,j.x+10,j.y+10,fill=civs[i].couleur)

class Controleur(object):
    def __init__(self):
        self.vue=Vue(self)
        #self.nom=tks.askstring("Dialogue d'entrée","Une chaîne svp!")
#        if self.nom:
#            self.modele=JeuOrion(self,self.nom)
#            self.serveur=xmlrpclib.ServerProxy('http://localhost:8000')
        self.vue.root.mainloop()
    def newDestination(self,evt):
        self.modele.newDestination(evt)
        
    def demarre(self):
        self.nom=self.vue.id.get()
        if self.nom:
            self.modele=JeuOrion(self,self.nom)
            self.serveur=xmlrpclib.ServerProxy('http://127.0.0.1:8000')
            univers=pickle.loads(self.serveur.nouveauJoueur(self.nom))
            self.modele.univers= univers
            self.ping()
            self.animate()
    def newAction(self,action):
        reponse=self.serveur.newAction(action)
        
    def ping(self):
        reponse=self.serveur.rienDeNeuf(self.nom)
        if reponse[0] <> "RIEN":
            for i in reponse:
                if i[1]=="civ":
                    civ=pickle.loads(i[2])
                    self.modele.ajouteCiv(i[0],civ)
                
                elif i[1]=="destination":
                    pos=pickle.loads(i[2])
                    self.modele.univers.updateCiv(i[0],i[1],pos)
#        else:
#            print "heures",reponse[1]
        if self.modele.univers.civs:
            self.modele.update()
            self.vue.drawMap(self.modele.univers.civs)
        self.vue.root.after(100,self.ping)
        
    def animate(self):
        if self.modele.univers.civs:
            self.modele.update()
            self.vue.drawMap(self.modele.univers.civs)
        self.vue.root.after(100,self.animate)
        
    def EnvoieMsg(self):
        if not self.modele.nom:
            self.modele.nom=raw_input("Identifiez vous svp!")
            if not self.modele.nom:
                print "Vous comprenez pas quand on vous explique???"
                return
        laligne=self.vue.lignemsg.get()
        rep=self.serveur.maLigne(self.modele.nom+": "+laligne)
        print "envoie reussi",rep

if __name__ == '__main__':
    controleur=Controleur()
    
